package com.atguigu.controller;

import com.atguigu.entity.Admin;
import com.atguigu.entity.Permission;
import com.atguigu.service.AdminService;
import com.atguigu.service.PermissionService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * @title：
 * @author：Yu
 * @create：2023-03-02 10:00
 * @description：
 */
@Controller
public class IndexController {
    @DubboReference
    private PermissionService permissionService;
    @DubboReference
    private AdminService adminService;

    @RequestMapping("/")
    public String toIndexPage(Map map) {
        //1.获取登录用户的信息，即springSecurity的User对象
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();

        //2.通过用户名查询该用户的id，为了通过adminId查找该用户的权限
        Admin admin = adminService.findAdminByUsername(user.getUsername());
        //3.查询该用户所拥有的权限
        List<Permission> permissionByadminId = permissionService.findPermissionByadminId(admin.getId());
        map.put("permissionList", permissionByadminId);
        return "frame/index";
    }

    @RequestMapping("/login")
    public String login() {
        return "frame/login";
    }

    @RequestMapping("/auth")
    public String auth() {
        return "frame/auth";
    }
}
